<template>
  <div>
    <div class="commodity">
      <h1>品类管理</h1>
      <el-button type="primary" @click="add">添加品类</el-button>
    </div>
    <p class="commodity_center">当前商品分类ID：{{ id }}</p>
    <el-table :data="tableData" border style="width: 1250px" class="tab" stripe>
      <el-table-column prop="id" label="品类ID" width="180"> </el-table-column>
      <el-table-column prop="name" label="品类名称" width="550">
      </el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button class="btn" @click="amend(scope.row)"
              >修改名称</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="请输入新的品类名称" :visible.sync="flag" width="30%">
      <el-form :model="form">
        <el-form-item label="">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { hhh, kindName } from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      form: {
        name: ""
      },
      id: "",
      flag: false,
      cid: ""
    };
  }, //方法

  methods: {
    add() {
      this.$router.push("/kindadd");
    },
    amend(i) {
      this.flag = true;
      this.id = i.id;
      this.form.name = i.name;
    },
    update() {
      this.flag = false;
      kindName(this.id,this.form.name).then(res => {
        console.log(res);
        if (res.data.status == 0) {
          alert(`${res.data.data}`);
          hhh(this.$route.query.id).then(res => {
            this.tableData = res.data.data;
          });
        } else {
          alert("更改失败");
        }
      });
    }
  }, //挂载后生命周期函数

  mounted() {
    hhh(this.$route.query.id).then(res => {
      this.tableData = res.data.data;
    });
  }
};
</script>

<style lang="scss" scoped>
.commodity {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 1260px;
  height: 50px;
  margin: 0 auto;
  border-bottom: 1px solid #ccc;
  h1 {
    font-size: 25px;
  }
}
.commodity_center {
  padding: 20px 0 0 40px;
}
.tab {
  margin: 20px auto 0;
}
</style>
